﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class FrmHoaDonTienDien : PRE.catalog.FrmBase
    {
        
        DAL.HoaDonTienDienDAL _dal = new DAL.HoaDonTienDienDAL();
        BLL.HoaDonTienDienBLL _bll = new BLL.HoaDonTienDienBLL();
        public FrmHoaDonTienDien()
        {
            InitializeComponent();
        }


        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus_hdtd(false);

            txtthuegtgt.ResetText();

            txtmahdtd.Text=_bll.create_ID();
            txttongtien.ResetText();
            txttongkydien.ResetText();
            dtengayghi.EditValue = DateTime.Now;
            cboma_nv.EditValue=-1;
            cboma_hd.EditValue = -1;
            cboTrangThai.EditValue = -1;
            cboDienKe.EditValue = -1;
            cbongayghisodien.EditValue = -1;
            txtmahdtd.Focus();

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus_hdtd();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtmahdtd.Text.Trim();

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    var o = new DAL.Entities.Hoa_Don_Tien_Dien()
                    {
                        madk=(cboDienKe.EditValue+"").Trim(),
                        ngayghisodien=Convert.ToDateTime(cbongayghisodien.EditValue)
                    };
                    var okoka = _dal.Update_chisodienke(o, 0);
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus_hdtd(false);
            dtengayghi.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            var frm = new Report.ReportBase();
            frm.Text = "In: " + Text;
            var rpt = new Report.XRHoadon();
            string id = grvMain.GetFocusedRowCellValue("Ma_HDTD") + "";
            //rpt.parDau.Value = txtDau.Text;
            //rpt.parRot.Value = txtRot.Text;

            rpt.DataSource = _dal.select_HDTDReport(id);
            frm.SetReport(rpt);
            frm.Show();
            frm.Activate();

            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {

            
            if (IsAdd)
            {
                var o = new DAL.Entities.Hoa_Don_Tien_Dien()
                {
                    ma_hdtd = txtmahdtd.Text.Trim(),
                    ngayghi = Convert.ToDateTime(dtengayghi.EditValue),
                    thuegtgt = float.Parse(("0" + txtthuegtgt.Text).Trim()),
                    tongkydien = Convert.ToInt32("0"+txttongkydien.Text.Trim()),
                    tongtien = float.Parse(("0"+ txttongtien.Text).Trim()),
                    ma_nv = (cboma_nv.EditValue +"").Trim(),
                    ma_hd= (cboma_hd.EditValue +"").Trim(),
                    matrth=(cboTrangThai.EditValue+"").Trim(),
                    madk=(cboDienKe.EditValue+"").Trim(),
                    ngayghisodien=Convert.ToDateTime(cbongayghisodien.EditValue)

                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    var okoka = _dal.Update_chisodienke(o,1);
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_hdtd(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_hdtd(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {


                var o = new DAL.Entities.Hoa_Don_Tien_Dien()
                {
                    ma_hdtd = txtmahdtd.Text.Trim(),
                    ngayghi = Convert.ToDateTime(dtengayghi.EditValue),
                    thuegtgt = float.Parse(("0" + txtthuegtgt.Text).Trim()),
                    tongkydien = Convert.ToInt32("0" + txttongkydien.Text.Trim()),
                    tongtien = float.Parse(("0" + txttongtien.Text).Trim()),
                    ma_nv = (cboma_nv.EditValue + "").Trim(),
                    ma_hd = (cboma_hd.EditValue + "").Trim(),
                    matrth=(cboTrangThai.EditValue+"").Trim(),
                    madk=(cboDienKe.EditValue+"").Trim(),
                    ngayghisodien = Convert.ToDateTime(cbongayghisodien.EditValue)
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    // MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_hdtd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_hdtd(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            //txtmahdtd.Text = null;
            txttongtien.Text = null;
            txttongkydien.Text = null;
            txtthuegtgt.Text = null;

            dtengayghi.EditValue = DateTime.Now;
            cboma_hd.EditValue = -1;
            cboma_nv.EditValue = -1;
            cboDienKe.EditValue = -1;
            cboTrangThai.EditValue = -1;
            cbongayghisodien.EditValue = -1;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtmahdtd.DataBindings.Clear();

            dtengayghi.DataBindings.Clear();
            txtthuegtgt.DataBindings.Clear();
            txttongkydien.DataBindings.Clear();
            txttongtien.DataBindings.Clear();
            cboma_nv.DataBindings.Clear();
            cboma_hd.DataBindings.Clear();
            cboTrangThai.DataBindings.Clear();
            cboDienKe.DataBindings.Clear();
            cbongayghisodien.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtmahdtd.DataBindings.Add("EditValue", _dtb, ".Ma_HDTD");
            dtengayghi.DataBindings.Add("EditValue", _dtb, ".NgayGhi");
            txtthuegtgt.DataBindings.Add("EditValue", _dtb, ".ThueGTGT");
            txttongkydien.DataBindings.Add("EditValue", _dtb, ".TongKyDien");
            txttongtien.DataBindings.Add("EditValue", _dtb, ".TongTien");
            cboma_nv.DataBindings.Add("EditValue", _dtb, ".Ma_NV");
            cboma_hd.DataBindings.Add("EditValue", _dtb, ".Ma_HD");
            cboDienKe.DataBindings.Add("EditValue", _dtb, ".Ma_DK");
            cboTrangThai.DataBindings.Add("EditValue", _dtb, ".Ma_TrTh");
            cbongayghisodien.DataBindings.Add("EditValue", _dtb, ".Ngayghisodien");


            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txtmahdtd.Properties.ReadOnly = true;

            dtengayghi.Properties.ReadOnly = isReadOnly;
            txtthuegtgt.Properties.ReadOnly = isReadOnly;
            
            
            cboma_nv.Properties.ReadOnly = isReadOnly;
            cboma_hd.Properties.ReadOnly = isReadOnly;
            cboTrangThai.Properties.ReadOnly = isReadOnly;
            cboDienKe.Properties.ReadOnly = isReadOnly;
            cbongayghisodien.Properties.ReadOnly = isReadOnly;
            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //DataTable tmp = _dal3.Select_Current(DateTime.Now);
            //string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            _dtb = _dal.Select();


            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
            }

            base.LoadData();
        }
        #endregion

        private void FrmHoaDonTienDien_Load(object sender, EventArgs e)
        {
            DataTable _dtb1 = _dal.Select_nhanvien();
            cboma_nv.Properties.DataSource = _dtb1;

            DataTable _dtb2 = _dal.Select_khachhang();            
            cboma_hd.Properties.DataSource = _dtb2;

            DataTable _dtb3 = _dal.Select_trangthai();
            cboTrangThai.Properties.DataSource = _dtb3;

            DataTable _dtb4 = _dal.Select_dienke();
            cboDienKe.Properties.DataSource = _dtb4;

            DataTable _dtb5 =_dal.Select_ngayghidien();
            cbongayghisodien.Properties.DataSource = _dtb5;

            txttongkydien.Properties.ReadOnly = true;
            txttongtien.Properties.ReadOnly = true;
            PerformRefresh();
        }

        private void cboDienKe_EditValueChanged(object sender, EventArgs e)
        {
            if (IsAdd)
            {
                string id = (cboDienKe.EditValue + "").Trim();
                cbongayghisodien.Properties.DataSource = _dal.select_ngayghisodien_theo_DK(id);
                

            }
        }

        private void cboma_hd_EditValueChanged(object sender, EventArgs e)
        {
            if (IsAdd)
            {
                string a = (cboma_hd.EditValue + "").Trim(); ;
                cboDienKe.Properties.DataSource= _dal.select_DK_theo_HD(a);
            }
        }

        private void cbongayghisodien_EditValueChanged(object sender, EventArgs e)
        {
            //string thue = txtthuegtgt.Text.Trim();
            //string ma_hd = ;
            
            var o = new DAL.Entities.Hoa_Don_Tien_Dien()
            {
                ma_hd=(cboma_hd.EditValue + "").Trim(),
                thuegtgt=float.Parse(txtthuegtgt.Text.Trim()),
                madk=(cboDienKe.EditValue + "").Trim(),
                ngayghisodien=Convert.ToDateTime(cbongayghisodien.EditValue)
            };
            string kydien = _bll.sokydien(o.madk,o.ngayghisodien).Trim();
            txttongkydien.Text = kydien;
            txttongtien.Text = _bll.tongsotien(o.ma_hd, kydien, o.thuegtgt);
        }



    }
}
